package com.aiyige.page.my.customer.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aiyige.R;
import com.aiyige.base.BaseFragment;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.model.request.GetCustomerOrderListRequest;
import com.aiyige.model.response.CustomerOrderListResponse;
import com.aiyige.page.my.customer.adapter.CustomerOrderRecordListAdapter;
import com.aiyige.page.my.customer.model.CustomerEntity;
import com.aiyige.page.my.customer.model.CustomerOrderEntity;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.widget.CommonDataView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Call;

/**
 * 客户订单记录
 * Created by xiachao on 2018/3/28.
 */
public class CustomerOrderRecordDataPage extends BaseFragment implements CommonDataView.RetrofitStub {
    View rootView;
    CustomerOrderRecordListAdapter adapter;
    @BindView(R.id.cdv)
    CommonDataView cdv;
    CustomerEntity customerEntity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.page_customer_order_record_list, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    public static CustomerOrderRecordDataPage newInstance() {
        CustomerOrderRecordDataPage fragment = new CustomerOrderRecordDataPage();
        return fragment;
    }

    @Override
    protected void initData() {
        adapter = new CustomerOrderRecordListAdapter();
        cdv.config(adapter, this);
//        cdv.addItemDecoration();
//        cdv.addItemDecoration(new MyRecyclerViewDivider(
//                this, LinearLayoutManager.VERTICAL, UIHelper.dip2px(getActivity(), 1), R.color.zwcolor_gray_e3e3e3));
        cdv.addItemDecoration(6, R.color.zwcolor_gray_f5f7f9);
        cdv.setRecyclerViewBgColor(ContextCompat.getColor(getActivity(), R.color.zwcolor_gray_f5f7f9));
//        adapter.setNewData(Arrays.asList(new CustomerOrderEntity(), new CustomerOrderEntity(), new CustomerOrderEntity(), new CustomerOrderEntity(), new CustomerOrderEntity(), new CustomerOrderEntity(), new CustomerOrderEntity(), new CustomerOrderEntity(), new CustomerOrderEntity(), new CustomerOrderEntity()));
//        cdv.hideCoverLayout();
//        cdv.clearRequestState();
//        adapter.loadMoreComplete();
//        adapter.setEnableLoadMore(false);
//        adapter.loadMoreEnd(true);
        cdv.refresh();
    }

    @Override
    public Call<ResponseBody> call(long currentPage) {
        return GetCustomerOrderListRequest.newBuilder().customerId(customerEntity.getId()).pageNum(currentPage+"").build();
    }

    @Override
    public CommonDataView.HandleResult handleResponse(String respJson, List resultList) {
        String respJsonTest = respJson;
        Log.v("XiachaoTest", respJsonTest);
        try {
            CustomerOrderListResponse homeResponse = JsonUtil.toObject(respJson, CustomerOrderListResponse.class);
            if (ListUtil.isEmpty(homeResponse.getContent())) {
                resultList.addAll(homeResponse.getContent());
                return new CommonDataView.HandleResult(homeResponse.isLast());
            } else {
                List<CustomerOrderEntity> responseList = homeResponse.getContent();
                resultList.addAll(responseList);
                return new CommonDataView.HandleResult(homeResponse.isLast());
            }

        } catch (Exception e) {
            return new CommonDataView.HandleResult(e.getMessage());
        }
    }

    public void setCustomerEntity(CustomerEntity customerEntity) {
        this.customerEntity = customerEntity;

    }
}
